package com.zhsq.zlc.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zhsq.zlc.entity.*;
import com.zhsq.zlc.service.combobox.ComboboxService;
import com.zhsq.zlc.service.log.LogService;
import com.zhsq.zlc.service.user.UserService;
import com.zhsq.zlc.util.Constant;
import com.zhsq.zlc.util.MD5Util;
import com.zhsq.zlc.util.PageUtil;
import com.zhsq.zlc.util.SessionInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

	@Value("${default.user.password}")
	private String defaultPassword;

	@Autowired
	private UserService userService;

	@Autowired
	private ComboboxService comboboxService;

	@Autowired
	private LogService logService;
	/**
	 * 用户列表
	 * @param entity
	 * @param pageCurrent
	 * @param pageSize
	 * @param pageCount
	 * @param model
	 * @return
	 */
	@RequestMapping("/zhsq/userManage_{pageCurrent}_{pageSize}_{pageCount}")
	public String userManage(UserEntity entity, @PathVariable Integer pageCurrent, @PathVariable Integer pageSize, @PathVariable Integer pageCount, Model model) throws Exception{
		// 判断
		if(pageSize == 0) pageSize = 10;
		if(pageCurrent == 0) pageCurrent = 1;
		int rows = userService.count(entity);
		if(pageCount == 0) pageCount = rows%pageSize == 0 ? (rows/pageSize) : (rows/pageSize) + 1;

		//查询
		entity.setStart((pageCurrent - 1)*pageSize);
		entity.setEnd(pageSize);
		List<UserEntity> list = userService.list(entity);
		if(list!=null && list.size()>0){
			for (int i=0;i<list.size();i++){
				list.get(i).setDataIndex(entity.getStart()+i+1);
			}
		}
		//输出
		model.addAttribute("list", list);
		String pageHTML = PageUtil.getPageContent("userManage_{pageCurrent}_{pageSize}_{pageCount}?realName="+entity.getRealName()+"&mobile="+entity.getMobile()+"&roleId="+entity.getRoleId(), pageCurrent, pageSize, pageCount);
		model.addAttribute("pageHTML",pageHTML);
		model.addAttribute("roles",comboboxService.listRolesCombobox(null));
		model.addAttribute("user",entity);
		return "zhsq/userManage";
	}

	/**
	 * 用户新增、修改跳转
	 * @param model
	 * @param entity
	 * @return
	 */
	@PostMapping("/zhsq/userEditGetEntity")
	public String userEditGetEntity(Model model,UserEntity entity) throws Exception{
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("type","user_status");
		List<ComboboxEntity> list=comboboxService.listDictionarysCombobox(params);
		model.addAttribute("userStatus",list);
		if(entity.getId()!=null){
			model.addAttribute("user",userService.getUserById(entity));
		}else{
			model.addAttribute("user",new UserEntity());
		}
		model.addAttribute("roles",comboboxService.listRolesCombobox(null));
		return "zhsq/userEdit";
	}

	/**
	 * 用户新增、修改提交
	 * @param model
	 * @param entity
	 * @param httpSession
	 * @return
	 */
	@PostMapping("/zhsq/userEdit")
	public String userEditPost(Model model, UserEntity entity, HttpSession httpSession, HttpServletRequest request)throws Exception {
		HttpSession session = request.getSession();
		SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
		if(entity.getId()!=null &&  entity.getId()!=0){
			userService.edit(entity);
			this.logService.insertLog(LogEntity.EDIT_TYPE, "修改用户数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
		} else {
			entity.setPassword(MD5Util.getMD5Str(defaultPassword));
			userService.add(entity);
			this.logService.insertLog(LogEntity.INSERT_TYPE, "添加用户数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
		}
		return "redirect:userManage_0_0_0";
	}

	@ResponseBody
	@PostMapping("/zhsq/userDelete")
	public ResultObject<Object> userDelete(IDEntity entity, HttpServletRequest request) throws Exception{
		HttpSession session = request.getSession();
		SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
		userService.delete(entity.getIntID());
		ResultObject<Object> object = new ResultObject<Object>(Constant.Code01, Constant.Msg01, null, null);
		return object;
	}


}
